using System;
using System.ServiceProcess;
using log4net;

namespace Sedna.Core.Scheduler
{
    public partial class Scheduler : ServiceBase
    {
        private ILog log = LogManager.GetLogger(typeof(Scheduler));
        private ISchedulerConfiguration configuration;


        public Scheduler(ISchedulerConfiguration configuration)
        {
            InitializeComponent();

            this.configuration = configuration;

        }

        protected override void OnStart(string[] args)
        {
            log.Info("Starting scheduler");
            
            try
            {
                foreach (ICondition task in configuration.Conditions)
                {
                    task.Enabled = true;
                }
            }
            catch (Exception e)
            {
                log.Error("Error starting scheduler", e);
                throw;
            }
            
            log.Info("Scheduler started");
        }

        protected override void OnStop()
        {
            log.Info("Stopping scheduler");
            
            try
            {
                foreach (ICondition task in configuration.Conditions)
                {
                    task.Enabled = false;
                }

            }
            catch (Exception e)
            {
                log.Error("Error stopping scheduler", e);
                throw;
            }
            
            log.Info("Scheduler stopped");
        }

    }
}
